<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Home extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('m_location');
    }

    function index() {
        $data['title'] = "Travelina";
        $data['page'] = "home";
        $data['message'] = null;
        $data['location'] = $this->m_location->get_all_location();
        $this->load->view('home', $data);
    }
    function search_process() {
        if($this->input->post('btn_src_location') != ""){
            $id_loc = $this->input->post('src_location');
            redirect("Home/search/$id_loc");
        }else{
            redirect('Home');
        }
    }
    function search($param, $id=0) {
        if($param !=""){
            $data['page'] = "search";
            $data['logo'] = "";
            $data['location'] = "All Location";
            $data['title'] = "Search Location : All Location";
            $data['select_location'] = $this->m_location->get_all_location();
            
            $this->load->library('pagination');
            $this->load->model('m_post');
            //pengaturan pagination

            $config['num_links'] = 2;

            $config['next_link'] = '<i class="fa fa-forward"></i>';
            $config['prev_link'] = '<i class="fa fa-backward"></i>';
            $config['first_link'] = '<i class="fa fa-step-backward"></i>';
            $config['last_link'] = '<i class="fa fa-step-forward"></i>';

            $config['full_tag_open'] = '<ul class="pagination">';
            $config['full_tag_close'] = '</ul>';
            $config['first_tag_open'] = '<li>';
            $config['first_tag_close'] = '</li>';
            $config['next_tag_open'] = '<li>';
            $config['next_tag_close'] = '</li>';
            $config['prev_tag_open'] = '<li>';
            $config['prev_tag_close'] = '</li>';
            $config['cur_tag_open'] = '<li class="active"><a href="#" style="color:#fff;">';
            $config['cur_tag_close'] = '</li>';
            $config['num_tag_open'] = '<li>';
            $config['num_tag_close'] = '</li>';
            $config['last_tag_open'] = '<li>';
            $config['last_tag_close'] = '</li>';


            if($param == "all"){
                $this->db->select('*');
                $this->db->from('post');
                $jml = $this->db->get();
                $config['base_url'] = site_url().'/Home/search/all/';
                $config['uri_segment'] = 4;
                $config['per_page'] = '5';

                //$data['post_by_location'] = $this->m_post->select_all_post();
                $data['post_by_location'] = $this->m_post->get_post_page($config['per_page'], $id);
                $data['map_latitude'] = "-2.548926";
                $data['map_longitude'] = "118.0148634";
                
                $this->load->model('m_post');
                $data['map_zoom'] = "5";
            }else{
                $this->db->select('*');
                $this->db->from('post');
                $this->db->join('location', 'location.id_location = post.id_location');
                $this->db->where('location_name', $param); 
                $jml = $this->db->get();

                $config['base_url'] = site_url()."/Home/search/$param/";
                $config['uri_segment'] = 4;
                $config['per_page'] = '5';

                $data['title'] = "Search Location : $param";
                $data['location'] = $param;
                $this->load->model('m_post');
                $data['post_by_location'] = $this->m_post->select_post_by_location($param, $config['per_page'], $id);
                //$data['post_by_location'] = $this->m_post->select_post_by_location($param);

                $data['global_map'] = $this->m_location->get_by_location_name($param);
                if($data['global_map']['global_latitude'] != "" || $data['global_map']['global_longitude'] != ""){
                    $data['map_latitude'] = $data['global_map']['global_latitude'];
                    $data['map_longitude'] = $data['global_map']['global_longitude'];
                    $data['map_zoom'] = "10";
                }  else {
                    $data['map_latitude'] = "-2.548926";
                    $data['map_longitude'] = "118.0148634";
                    $data['map_zoom'] = "5";
                }
            }
            $config['total_rows'] = $jml->num_rows();
               //inisialisasi config
            $this->pagination->initialize($config);

            $data['halaman'] = $this->pagination->create_links();
            $data['sidebar'] = $this->getRecentPost();
            
            $this->load->view('home', $data);
        }else{
            redirect('Home');
        }
    }
    
    function post($param) {
        if($param !=""){
            $this->load->model('m_post');
            $data_getter = $this->m_post->select_post_byId($param);
            
            //-S- Detail Post Controll
            $data['title'] = $data_getter['post_title']." - Travelina";
            $data['location'] = $data_getter['location_name'];
            $data['post_thumb'] = $data_getter['thumbnail'];
            $data['post_author'] = $data_getter['fullname_user'];
            $data['post_date'] = $data_getter['post_date'];
            $data['post_category'] = $data_getter['category_name'];
            $data['post_content'] = $data_getter['post_content'];
            //-E- Detail Post Controll
            
            //-S- Author Bio
            $data['post_author_bio'] = $data_getter['bio'];
            $data['post_author_ava'] = $data_getter['user_ava'];
            $data['post_author_fb'] = $data_getter['fb_acc'];
            $data['post_author_twitter'] = $data_getter['twitter_acc'];
            $data['post_author_gplus'] = $data_getter['gplus_acc'];
            //-F- Author Bio
            
            $data['related'] = $this->m_post->select_by_category($data_getter['id_category']);
            //print_r($data['related']);
            
            $data['select_location'] = $this->m_location->get_all_location();
            $data['page'] = "show_post";
            $data['logo'] = "";
            
            if($data_getter['latitude'] != "" || $data_getter['longitude'] != ""){
                $data['map_latitude'] = $data_getter['latitude'];
                $data['map_longitude'] = $data_getter['longitude'];
                $data['map_zoom'] = "15";
            }  else {
                $data['map_latitude'] = "-2.548926";
                $data['map_longitude'] = "118.0148634";
                $data['map_zoom'] = "5";
            }
            
            //print_r($this->m_post->select_post_byId($param));
            $data['sidebar'] = $this->getRecentPost();
            $this->load->view('home', $data);
            //$this->load->view('lokasi');
        }else{
            redirect('Home');
        }
    }

    function register() {
        if($this->input->post('btn_reg') != ""){
            $this->load->library('form_validation');
            
            $this->form_validation->set_rules('reg_username', 'Username', 'required|alpha_dash|min_length[4]|is_unique[user.username_user]|max_length[20]');
            $this->form_validation->set_rules('reg_fullname', 'Full Name', 'required|min_length[6]|max_length[50]');
            $this->form_validation->set_rules('reg_password', 'Password', 'required');
            $this->form_validation->set_rules('reg_2ndpassword', 'Re-type Password', 'required|matches[reg_password]');
            $this->form_validation->set_rules('agree', 'Agreement', 'required');
            
            if ($this->form_validation->run() != FALSE){
                $user_data['username'] = $this->input->post('reg_username');
                $user_data['fullname'] = $this->input->post('reg_fullname');
                $user_data['password'] = md5($this->input->post('reg_password'));
                $user_data['facebook'] = "http://facebook.com/".$this->input->post('reg_fb');
                $user_data['twitter'] = "http://twitter.com/".$this->input->post('reg_twitter');
                $user_data['gplus'] = "http://google.com/+".$this->input->post('reg_gplus');
                
                $this->load->model('m_user');
                $cek = $this->m_user->insert_user($user_data);
                
                if($cek == true){
                  $data['message'] = "1";
              }else{
                  $data['message'] = "0"; 
                  $data['error'] = "Register Failed. Try Once More";
              }
          }else{
              $data['message'] = "0";
              $data['error'] = validation_errors();
          }

      }

      $data['title'] = "Register New User";
      $data['logo'] = "";
      $data['page'] = "register";
      $this->load->view('home', $data);
  }

  private function getRecentPost(){
    $this->load->model('m_post');
    $result = $this->m_post->select_all_post();

        if(count($result) >= 5){
            for($i=0; $i<5; $i++){
                $result_new[$i] = $result[$i];
            }
        }else{
            for($i=0; $i<count($result); $i++){
                $result_new[$i] = $result[$i];
            }
        }
        return $result_new;
    }
}
